﻿use actix_web::http::header::ContentType;
use actix_web::web;
use actix_web::HttpResponse;
use sqlx::PgPool;

use crate::session_state::TypedSession;
use crate::utils::{e500, get_username, see_other};

#[tracing::instrument(name = "Get admin dashboard page", skip(session, pool))]
pub async fn admin_dashboard(
    session: TypedSession,
    pool: web::Data<PgPool>,
) -> Result<HttpResponse, actix_web::Error> {
    let username = if let Some(user_id) = session.get_user_id().map_err(e500)? {
        get_username(user_id, &pool).await.map_err(e500)?
    } else {
        return Ok(see_other("/login"));
    };
    Ok(HttpResponse::Ok()
        .content_type(ContentType::html())
        .body(format!(
            r#"<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equivv="content-type" content="text/html; charset=utf-8">
	<title>Admin dashboard</title>
</head>
<body>
	<p>Welcome {username}!</p>
	<p>Available actions:</p>
	<ol>
		<li><a href="/admin/password">Change password</a></li>
		<li>
			<form name="logoutForm" action="/admin/logout" method="post">
				<input type="submit" value="Logout"/>
			</form>
		</li>
	</ol>
</body>
</html>"#
        )))
}
